<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Controller_Friend extends CI_Controller {

	public function __construct() {
		parent::__construct ();
		require(APPPATH.'helpers/protocal.php');
		$this->load->model ( 'Model_Friend', 'Friend' );
		
	}

	public function search_user() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal']);
		$username = $_GET['username'];
		$query = $this->Friend->search_user_by_username($username);
		foreach ($query->result_array() as $row) {
			$result->user_list[] = $row;
		}
		$this->output->set_output(json_encode($result));
	}
	public function add_attention() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal']);
		$inviter = $_GET['inviter'];
		$invitee = $_GET['invitee'];
		$result->invitee = $invitee;
		$result->is_new = 1;

		if ($this->Friend->is_attention($inviter, $invitee)) {
			$result->return_code = 10011;
			goto label_return;
		}
		
		require(BASEPATH.'helpers/date_helper.php');	
		$time = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		if (!$this->Friend->add_attention($inviter, $invitee, $time )) {
			$result->return_code = 1;
		}
		else {
			$query = $this->Friend->get_attention($inviter, $invitee);
			foreach ($query->result_array() as $row) {
				$result->user_list[] = $row;
			}
		}
	label_return:
		$this->output->set_output(json_encode($result));
	}
	public function add_attention_by_username()
	{
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal']);
		$inviter = $_GET['inviter'];
		$invitee_name = $_POST['invitee_name'];

		$this->load->model ( 'Model_User', 'User' );
		$user = $this->User->get_user_by_name( $invitee_name );
		if ($user == null) {
			$result->return_code = 10010;
			goto label_return;
		}
		if ($inviter == $user->userid) {
			$result->return_code = 10012;
			goto label_return;
		}
		$result->invitee = $user->userid;
		$result->is_new = 1;
		if ($this->Friend->is_attention($inviter, $user->userid)) {
			$result->return_code = 10011;
			goto label_return;
		}
		require(BASEPATH.'helpers/date_helper.php');	
		$time = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		if (!$this->Friend->add_attention($inviter, $user->userid, $time)) {
			$result->return_code = 1;
		}
		else {
			$query = $this->Friend->get_attention($inviter, $user->userid);
			foreach ($query->result_array() as $row) {
				$result->user_list[] = $row;
			}
		}
	label_return:
		$this->output->set_output(json_encode($result));
	}
	public function remove_attention() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$inviter = $_GET['inviter'];
		$invitee = $_GET['invitee'];
		$result->invitee = $invitee;
		if (!$this->Friend->remove_attention($inviter, $invitee)) {
			$result->return_code = 1;
		}
		$result->invitee = $invitee;
		$this->output->set_output(json_encode($result));
	}
	public function get_attention_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$result->is_new = 0;
		//$type = get_login_type( $_GET['protocal']);
		$inviter = $_GET['inviter'];
		$time = $_GET['time'];
		require(BASEPATH.'helpers/date_helper.php');
		$ftime = gmstrftime ( "%Y-%m-%d %H:%M:%S", $time);
		$query = $this->Friend->get_attention_list($inviter, $ftime);
		foreach ($query->result_array() as $row) {
			$result->user_list[] = $row;
		}
		$this->output->set_output(json_encode($result));
	}
	public function get_fans_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal']);
		$invitee = $_GET['invitee'];
		$time = $_GET['time'];
		require(BASEPATH.'helpers/date_helper.php');
		$ftime = gmstrftime ( "%Y-%m-%d %H:%M:%S", $time);
		$query = $this->Friend->get_fans_list($invitee, $ftime);
		foreach ($query->result_array() as $row) {
			$result->user_list[] = $row;
		}
		$this->output->set_output(json_encode($result));
	}
	public function update_user_dress() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal'] );
		$userid = $_GET['userid'];
		$plist = $_POST['plist'];
		$md5sum = $_POST['md5sum'];
		
		$username = null;
		if (isset($_POST['username'])) {
			$username = $_POST['username'];
		}
		$home = $_POST['home'];
		$level = $_POST['level'];
		$age = $_POST['age'];
		$star = $_POST['star'];
		$weight = $_POST['weight'];
		$height = $_POST['height'];
		
		require(BASEPATH.'helpers/date_helper.php');
		if (!$this->Friend->update_user_dress($userid, $username, $plist, $md5sum, $home, $level, $age, $star, $weight, $height, strftime ( "%Y-%m-%d %H:%M:%S", now ()))) {
			$result->return_code = 1;
		}
		$this->output->set_output(json_encode($result));
	}
	public function modify_fans_state() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$result->inviter = $_GET['inviter'];
		$result->invitee = $_GET['invitee'];
		$result->flag = $_GET['flag'];
		
		$state = $this->Friend->get_friend_state($result->inviter, $result->invitee);
		if ($state == null) {
			$result->return_code = 10050;
			goto label_return;
		}
		if ($state == 2) {
			$result->return_code = 10051;
			goto label_return;
		}
		if ($result->flag != $state) {
			if (!$this->Friend->set_friend_state($result->inviter, $result->invitee, $result->flag) ) {
				$result->return_code = 1;
			}
		}
	label_return:
		$this->output->set_output(json_encode($result));
	}
	public function update_introduction() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$userid = $_GET['userid'];
		$msg = $_POST['msg'];
		require(BASEPATH.'helpers/date_helper.php');
		$ftime = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		if (!$this->Friend->update_introduction($userid, $msg, $ftime)) {			
			$result->return_code = 1;
		}
		$this->output->set_output(json_encode($result));
	}
	public function send_message() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$senderid = $_GET['senderid'];
		$receiverid = $_GET['receiverid'];
		$msg = $_POST['msg'];
		require(BASEPATH.'helpers/date_helper.php');
		$ftime = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		if (!$this->Friend->add_message($senderid, $receiverid, $msg, $ftime)) {
			$result->return_code = 1;
		}
		$this->output->set_output(json_encode($result));
	}
	public function remove_message() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$result->msgid = $_GET['msgid'];
		if ($this->Friend->remove_message($result->msgid)) {
			$result->return_code = 1;
		}
		$this->output->set_output(json_encode($result));
	}
	public function get_message_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$receiverid = $_GET['receiverid'];
		$time = $_GET['time'];
		require(BASEPATH.'helpers/date_helper.php');
		$ftime = gmstrftime ( "%Y-%m-%d %H:%M:%S", $time);
		log_message('debug', 'get_message_list start time '.$time.'  '.$ftime);
		$query = $this->Friend->get_message_list($receiverid, $ftime);
		foreach ($query->result_array() as $row) {
			$result->message_list[] = $row;
		}
		$this->output->set_output(json_encode($result));
	}
	public function get_friend_status() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$userid = $_GET['userid'];
		$time = $_GET['start_time'];
		require(BASEPATH.'helpers/date_helper.php');
		$ftime = gmstrftime ( "%Y-%m-%d %H:%M:%S", $time);
		log_message('debug', 'get_friend_status start time '.$time.'  '.$ftime);
		
	}
	public function index() {
		
		if (IS_MAINTAINING) {
			$result = null;
			$result->return_code = 10002;
			$this->output->set_output(json_encode($result));
			return ;
		}
		
		$protocal = get_protocal( $_GET['protocal'] );
		switch ($protocal) {
			case 1201 :
				$this->search_user ();
				break;
			case 1202 :
				$this->add_attention ();
				break;
			case 1203 :
				$this->get_attention_list ();
				break;
			case 1204 :
				$this->get_fans_list ();
				break;
			case 1205:
				$this->update_user_dress ();
				break;
			case 1206 :
				$this->remove_attention ();
				break;
			case 1207 :
				$this->modify_fans_state ();
				break;
			case 1208 :
				$this->update_introduction ();
				break;
			case 1209 :
				$this->send_message ();
				break;
			case 1210:
				$this->remove_message ();
				break;
			case 1211 :
				$this->get_message_list ();
				break;
			case 1212 :
				$this->add_attention_by_username();
				break;
			case 1213 :
				$this->get_friend_status();
				break;
			default :
				break;
		}
	}
}
